﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using Codeplex.Unity.AutoRegister;
using Codeplex.Unity.AutoRegister.Config;
using SampleApplication.Library;

namespace SampleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            // add extension
            container.AddNewExtension<AutoRegistExtension>();
            // configure extension
            var config = container.Configure<AutoRegistExtension>();
            // add assembly
            config.AddProvider(
                new AssemblyRegistTypeTraitProvider(typeof(GreeterClient).Assembly));

            Exec1(container);
            Console.WriteLine("----");
            Exec2(container);
            Console.WriteLine("----");
            Exec3(container);
        }

        private static void Exec1(UnityContainer container)
        {
            // annotation base regist and injection
            var client = container.Resolve<GreeterClient>();
            client.Execute();

            // annotation base LifetimeManager
            var singleton1 = container.Resolve<IGreeter>("Singleton");
            var singleton2 = container.Resolve<IGreeter>("Singleton");
            Console.WriteLine("singleton1 == singleton2 -> {0}", object.ReferenceEquals(singleton1, singleton2));
        }

        private static void Exec2(UnityContainer container)
        {
            // custom injection
            var control = container.Resolve<Control>();
            Console.WriteLine("control.ViewModel = {0}", control.DataContext);
        }

        public static void Exec3(UnityContainer container)
        {
            // custom injection
            var control = container.Resolve<Control>("InjectionMemberProviderSample");
            Console.WriteLine("control.ViewModel = {0}", control.DataContext);
        }
    }
}
